package com.venusj.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zhangjh
 * @date 2020/12/25
 * @desc 通过反射破坏单例结构
 */
public class MySingletonTest {


    public static void main(String[] args) {
        MySingleton instance = MySingleton.getInstance();

        try {
            Class<MySingleton> forName = (Class<MySingleton>) Class.forName("com.venusj.singleton.MySingleton");
            Constructor<MySingleton> declaredConstructor = forName.getDeclaredConstructor(null);
            declaredConstructor.setAccessible(true);
            MySingleton mySingleton = declaredConstructor.newInstance();

            System.out.println(mySingleton);
            System.out.println(instance);
            System.out.println(instance== mySingleton);

        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
